/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package perso;

import carte.Carte;
import ctrl.AbstractFactory;
import ctrl.command.CmdConvert;
import ctrl.command.CmdCreate;
import ctrl.command.CmdKill;
import ctrl.command.CmdMove;
import ctrl.command.Command;
import java.util.ArrayList;

/**
 *
 * @author Vincent
 */
public class Truand extends PersonnageTruand {

    public boolean isTaupe = false;
    public boolean isMale;
    

    public Truand(int id, Carte carte, boolean isMale) {
        super(id, carte);
        this.isMale = isMale;
        if (isMale) {
            setImage("/images/truandM.jpg");
            name = "Truand M";
        } else {
            setImage("/images/truandF.jpg");
            name = "Truand F";
        }
    }

    public void convertToTaupe() {
        setImage("/images/taupe.jpg");
        name = "Taupe";
        isTaupe = true;
    }

    @Override
    public Command doAction() {
        // CarteTruand map = (CarteTruand)carte;
        Command cmd = new CmdMove(this, px, py);
        int lower = 1;
        int higher = 5;
        int random = (int) (Math.random() * (higher - lower)) + lower;
        int pxDest = px;
        int pyDest = py;
        if (random == 1) {
            if (px + 1 < carte.getPerso().length) {
                pxDest++;
            }
        } else if (random == 2) {
            if (px - 1 > 0) {
                pxDest--;
            }
        } else if (random == 3) {
            if (py - 1 > 0) {
                pyDest--;
            }
        } else if (random == 4) {
            if (py + 1 < carte.getPerso()[0].length) {
                pyDest++;
            }
        }
        Personnage cible = carte.getPerso()[pxDest][pyDest];

        // Test si la case est libre
        if (cible == null) {
            cmd = new CmdMove(this, pxDest, pyDest);
            // Test si c'est lui qui est déjà sur la case
        } else if (cible.id == id) {
            return cmd;
        }


        if (isTaupe) {
            if (cible instanceof Chef) {
                cmd = new CmdKill(this);
            } else if (cible instanceof Truand) {
                if (!((Truand) cible).isTaupe) {
                    cmd = new CmdKill(cible);
                }
            }
        }  else {

            if (cible instanceof Agent) {
                cmd = new CmdConvert(this);
            } else if (cible instanceof Truand) {
                if (((Truand) cible).isMale != isMale) {
                    if (!isMale) {
                        cible = this;
                    }
                    if (py + 1 > carte.getPerso()[0].length) {
                        Personnage bebe = carte.getPerso()[cible.px][cible.py + 1];
                        if (bebe == null) {
                            cmd = new CmdCreate(this,cible.px,cible.py + 1);
                        }
                    }
                }
            }
        }
        return cmd;
    }

    @Override
    public void createPerso(ArrayList<Personnage> listperso, int px,int py) {
        Personnage p = null;
        int id = listperso.lastIndexOf(p);
        Truand t = new Truand(id,carte,AbstractFactory.getRandomBoolean());
        listperso.add(t);
        t.px=px;
        t.py=py;
        carte.getPerso()[px][py]=t;
    }

    @Override
    public void convert(ArrayList<Personnage> listPerso) {
        convertToTaupe();
    }
}
